# 深浅拷贝

# 浅拷贝: 拷贝最外层的对象, 内部的元素只拷贝了一个引用
# 深拷贝: 拷贝所有的对象, 包括内部的元素

print("-------------赋值----------------------")
# -------- 赋值 --------
li1 = [1, 2, 3, 4]
li2 = li1
print(f"li: {li1}")  # li: [1, 2, 3, 4]
print(f"li2: {li2}")  # li2: [1, 2, 3, 4]
li1.append(5)
print(f"after append li: {li1}")  # after append li: [1, 2, 3, 4, 5]
print(f"after append li2: {li2}")  # after append li2: [1, 2, 3, 4, 5]
print(f"memory address: {id(li1)}")  # 4297588480
print(f"memory address: {id(li2)}")  # 4297588480
# 赋值操作等于完全共享资源,一个值的变化会完全被另一个值共享

import copy

print("-------------浅拷贝----------------------")
# 浅拷贝: 数据半共享, 拷贝最外层的对象, 内部的元素只拷贝了一个引用
# 浅拷贝: 速度快, 占用内存少
li3 = [1, 2, 3, [4, 5, 6]]
li4 = copy.copy(li3)
print(f"origin li3: {li3}")  # [1, 2, 3, [4, 5, 6]]
print(f"origin li4: {li4}")  # [1, 2, 3, [4, 5, 6]]
print(f"memory address: {id(li3)}")  # 4310949184
print(f"memory address: {id(li4)}")  # 4311126336
li3.append(8)
print(f"after append li3: {li3}")  # after append li3: [1, 2, 3, [4, 5, 6], 8]
print(f"after append li4: {li4}")  # after append li4: [1, 2, 3, [4, 5, 6]]
li3[3].append(7)
print(
    f"after append inner li3: {li3}"
)  # after append inner li3: [1, 2, 3, [4, 5, 6, 7], 8]
print(
    f"after append inner li4: {li4}"
)  # after append inner li4: [1, 2, 3, [4, 5, 6, 7]]
print(f"memory address: {id(li3[3])}")  # 4336781248
print(f"memory address: {id(li4[3])}")  # 4336781248

print("-------------深拷贝----------------------")
# 深拷贝: 数据完全独立, 拷贝所有的对象, 包括内部的元素
# 深拷贝: 速度慢, 占用内存多
li5 = [1, 2, 3, [4, 5, 6]]
li6 = copy.deepcopy(li5)
print(f"origin li5: {li5}")  # [1, 2, 3, [4, 5, 6]]
print(f"origin li6: {li6}")  # [1, 2, 3, [4, 5, 6]]
li5.append(8)
print(f"after append li5: {li5}")  # after append li5: [1, 2, 3, [4, 5, 6], 8]
print(f"after append li6: {li6}")  # after append li6: [1, 2, 3, [4, 5, 6]]

li5[3].append(7)
print(
    f"after inner append li5: {li5}"
)  # after inner append li5: [1, 2, 3, [4, 5, 6, 7], 8]
print(f"after inner append li6: {li6}")  # [1, 2, 3, [4, 5, 6]]
